package lock2;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Lock2Demo {
	
	public static void main(String[] args) {
		InCrement inCrement = new InCrement(new ReentrantLock());
		for (int i = 0; i < 10; i++) {
			new Thread(()->{
				inCrement.increment();
			}).start();
		}
		while(Thread.activeCount() > 1)Thread.yield();
		System.out.println(inCrement.count);
	}
}
class InCrement{
	
	private Lock lock;
	
	public int count = 0;
	
	public InCrement(Lock lock) {
		this.lock = lock;
	}
	
	public void increment() {
		lock.lock();
		for (int i = 0; i < 100; i++) {
			count++;
		}
		lock.unlock();
	}
}
